layui.use(['form', 'layer', 'laydate'], function () {
    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        company_id = sessionStorage["company_id"];
    laydate.render({
        elem: '.time',
        format: 'yyyy年MM月dd日 HH:mm:ss'
    });
    laydate.render({
        elem: '.andtime',
        format: 'yyyy年MM月dd日 HH:mm:ss'
    });
    form.on("submit(editAssistAccount)", function (data) {
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候', { icon: 16, time: false, shade: 0.8 });
        const { assistAccountId: id, name, password, phone, peopleName, time, andtime, reason } = data.field
        var assistAccount = {
            id,
            name,
            password,
            phone,
            peopleName,
            time,
            andtime,
            reason,
            company_id
        }
        if (data.field.addOrUpdate == 0) {//添加
            $.ajax({
                url: baseUrl + "assistAccount/addAssistAccount.php",
                data: assistAccount,
                type: "POST",
                dataType: "text",
                success: function (data) {
                    console.log(data);
                    if (data == "1") {
                        message(index, '添加账户信息成功！')
                    } else {
                        message(index, '添加账户信息失败！')
                    }
                }
            })
        } else {//修改
            $.ajax({
                url: baseUrl + "assistAccount/updateAssistAccount.php",
                data: assistAccount,
                type: "POST",
                dataType: "text",
                success: function (data) {
                    if (data == "1") {
                        message(index, '修改账户信息成功！')
                    } else {
                        message(index, '修改账户信息失败！')
                    }
                }
            })
        }
        return false;
    })
    function message(index, msg) {
        setTimeout(function () {
            top.layer.close(index);
            top.layer.msg(msg);
            layer.closeAll("iframe");
            //刷新父页面
            parent.location.reload();
        }, 1000);
    }
})